/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.audit.service.impl;

import com.koron.audit.domain.Audit;
import com.koron.audit.mapper.AuditMapper;
import com.koron.audit.service.api.AuditService;
import com.koron.audit.utils.BusinessCode;
import com.koron.common.domain.DataBean;
import com.koron.workflow.bean.MessageEntity;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AuditServiceImpl implements AuditService {

    private final static  Logger logger= LoggerFactory.getLogger(AuditServiceImpl.class);

    @Override
    public Integer insert(Audit audit) {
        try(SessionFactory factory=new SessionFactory()){
            Integer value=factory.getMapper(AuditMapper.class).insert(audit);
            return value;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return 0;
        }
    }

    @Override
    public Integer batchInsert(List<Audit> auditList) {
        try(SessionFactory factory=new SessionFactory()){
            Integer value=factory.getMapper(AuditMapper.class).batchInsert(auditList);
            return value;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return 0;
        }
    }

    @Override
    public Integer update(Audit audit) {
        try(SessionFactory factory=new SessionFactory()){
            Integer value=factory.getMapper(AuditMapper.class).update(audit);
            return value;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return 0;
        }
    }

    @Override
    public Integer batchUpdate(List<Audit> auditList) {
        try(SessionFactory factory=new SessionFactory()){
            Integer value=factory.getMapper(AuditMapper.class).batchUpdate(auditList);
            return value;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return 0;
        }
    }

    @Override
    public Integer delete(String id) {
        try(SessionFactory factory=new SessionFactory()){
            Integer value=factory.getMapper(AuditMapper.class).delete(id);
            return value;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return 0;
        }
    }

    @Override
    public Audit queryOne(String id) {
        try(SessionFactory factory=new SessionFactory()){
            Audit audit=factory.getMapper(AuditMapper.class).queryOne(id);
            return audit;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public List<Audit> queryByCondition( String code,String title,String flowName,Date time, Integer state, Integer start, Integer rows) {
        try(SessionFactory factory=new SessionFactory()){
            List<Audit> list=factory.getMapper(AuditMapper.class).queryByCondition(code,title,flowName,state,time,getnowEndTime(time),start,rows);
            return list;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public Integer countByCondition( String code,String title,String flowName, Date time, Integer state) {
        try(SessionFactory factory=new SessionFactory()){
            Integer value=factory.getMapper(AuditMapper.class).countByCondition(code,title,flowName,state,time,getnowEndTime(time));
            return value;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return 0;
        }
    }

    @Override
    public MessageBean<DataBean<Audit>> queryByIdList(List<String> idList, String code,String title,String flowName,Integer state, Date time, Integer start,Integer rows, String keyword) {
        MessageBean<DataBean<Audit>> msg=new MessageEntity<>();
        try(SessionFactory factory=new SessionFactory()){
            List<Audit> list=factory.getMapper(AuditMapper.class).queryByIdList(idList,code,title,flowName,state,time,getnowEndTime(time),start,rows,keyword);
            Integer totalNumber=factory.getMapper(AuditMapper.class).countByIdList(idList,code,title,flowName,state,time,getnowEndTime(time),keyword);
            rows = rows == null ?totalNumber:( rows <= 0 ? 1 : rows);
            DataBean<Audit> dataBean=new DataBean<>();
            dataBean.setList(list);
            dataBean.setTotalNumber(totalNumber);
            Integer totalPage=(totalNumber>rows?(totalNumber%rows==0?totalNumber/rows:totalNumber/rows+1):1);
            dataBean.setTotalPage(totalPage);
            dataBean.setPageSize(rows);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            msg.setData(dataBean);
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<DataBean<Audit>> queryByInitiator(String initiatorCode, String code,String title, String flowName,Date time,
                                                         Integer state,Integer start,Integer rows,String keyword) {
        MessageBean<DataBean<Audit>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()) {
            if (StringUtils.isEmpty(initiatorCode)){
                msg.setCode(BusinessCode.NOT_LOGIN.getCode());
                msg.setDescription(BusinessCode.NOT_LOGIN.getDescription());
                return msg;
            }
            List<Audit> list=factory.getMapper(AuditMapper.class).queryByInitiator(initiatorCode,code,title,flowName,state,time,getnowEndTime(time),start,rows,keyword);
            Integer totalNumber=factory.getMapper(AuditMapper.class).countByInitiator(initiatorCode,code,title,flowName,state,time,getnowEndTime(time),keyword);
            rows = rows == null ?totalNumber:( rows <= 0 ? 1 : rows);
            Integer totalPage=totalNumber>rows?(totalNumber%rows==0?totalNumber/rows:totalNumber/rows+1):1;
            DataBean<Audit>dataBean=new DataBean<>();
            dataBean.setList(list);
            dataBean.setTotalNumber(totalNumber);
            dataBean.setTotalPage(totalPage);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription("查询发起审批列表成功");
            msg.setData(dataBean);
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

	@Override
	public Audit queryByProcessId(String processInstId) {
		try(SessionFactory factory=new SessionFactory()){
            Audit audit=factory.getMapper(AuditMapper.class).queryByProcessId(processInstId);
            return audit;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return null;
        }
	}

    @Override
    public Audit queryByBusinessIds(String businessIds,Date updateTime) {
        try(SessionFactory factory=new SessionFactory()){
            Audit audit=factory.getMapper(AuditMapper.class).queryByBusinessIds(businessIds,updateTime);
            return audit;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public Audit getAuditByBusinessId(String businessId) {
        try(SessionFactory factory=new SessionFactory()){
            Audit audit=factory.getMapper(AuditMapper.class).getAuditByBusinessId(businessId);
            return audit;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            return null;
        }
    }


    private  Date getnowEndTime(Date date) {
        if (null!=date){
            Calendar todayEnd = Calendar.getInstance();
            todayEnd.setTime(date);
            todayEnd.set(Calendar.HOUR_OF_DAY, 23);
            todayEnd.set(Calendar.MINUTE, 59);
            todayEnd.set(Calendar.SECOND, 59);
            todayEnd.set(Calendar.MILLISECOND, 999);
            return todayEnd.getTime();
        }else{
            return null;
        }
    }
}
